package com.carlinfo.stu_ques.users.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.carlinfo.stu_ques.users.dao.ILevelDao;
import com.carlinfo.stu_ques.users.dao.IStudentDao;
import com.carlinfo.stu_ques.users.dao.impl.LevelDaoImpl;
import com.carlinfo.stu_ques.users.dao.impl.StudentDaoImpl;
import com.carlinfo.stu_ques.users.pojo.Level;
import com.carlinfo.stu_ques.users.pojo.Students;
import com.carlinfo.stu_ques.users.service.IUsersService;

public class UsersServiceImpl implements IUsersService
{

	private IStudentDao studentDao = new StudentDaoImpl();

	private ILevelDao levelDao = new LevelDaoImpl();

	@Override
	public int saveOneStudentService(Students stu)
	{	
		return this.studentDao.saveOneDao(stu);
	}

	@Override
	public void updateOneStudentService(Students stu)
	{
		this.studentDao.updateOneDao(stu);
	}

	@Override
	public void deleteOneStudentService(Students stu)
	{
		this.studentDao.deleteOneDao(stu);
	}

	@Override
	public List<Students> selectListStudentService()
	{
		List<Students> list = this.studentDao.selectListDao();
		for (Students students : list)
		{
			double sorce = students.getScore();
			List<Level> li = selectListLevelService();
			for (Level level : li)
			{
				if(sorce>=level.getStartMarks() && sorce<=level.getFinalMarks()) {
					students.setLevel(level);
				}
			}
		}
		return list;
	}

	@Override
	public Students selectOneStudentService(Map<String, Object> condMap)
	{
		return this.studentDao.selectOneDao(condMap);
	}

	@Override
	public String printTitleStu()
	{
		/* id,姓名,身高,创建时间,更新时间,爱好 */
		return "id\t姓名\t性别\t生日\t\t毕业院校\t\t\t分数\t等级\t总次数\t答对次数\t答错次数\t准确率\t今日正确\t今日答错\t今日总次数\t今日正确率\t创建时间\t\t\t更新时间";
	}

	/* ==========================等级=========================== */
	@Override
	public int saveOneLevelService(Level level)
	{
		return levelDao.saveOneDao(level);
	}

	@Override
	public void updateOneLevelService(Level level)
	{
		this.levelDao.updateOneDao(level);
	}

	@Override
	public void deleteOneLevelService(Level level)
	{
		this.levelDao.deleteOneDao(level);
	}

	@Override
	public List<Level> selectListLevelService()
	{
		return this.levelDao.selectListDao();
	}

	@Override
	public Level selectOneLevelService(Map<String, Object> condMap)
	{
		return this.levelDao.selectOneDao(condMap);
	}

	@Override
	public String printTitleLevel()
	{
		return "id\t等级\t\t初始分数\t\t结束分数\t\t创建时间\t\t\t更新时间";
	}

	@Override
	public int selectMaxIdService()
	{
		return this.studentDao.selectMaxId();
	}

	@Override
	public boolean askIsStudentService(Students students)
	{
		if(students==null) {
			return false;
		}
		int maxCount = 0;
		int askMaxCount = 0;
		boolean flag = true;
		boolean askFlag = true;
		List<Students> list = this.selectListStudentService();
		for (Students student : list)
		{
			if (student.getTotalCount() > maxCount)
			{
				maxCount = (int) student.getTotalCount();
			}
			if (askMaxCount == 0 && flag)
			{
				askMaxCount = (int) student.getTotalCount();
				flag = false;
			}
			if (askMaxCount != maxCount)
			{
				askFlag = false;
			}

		}
		if (students.getTotalCount() < maxCount || maxCount == 0 || askFlag)
		{
			return true;
		}
		return false;
	}
	
	@Override
	public boolean studentDbTofile(String path)
	{
		return this.studentDao.dbToFile(path);
	}

	@Override
	public boolean studentFileToDb()
	{
		return this.studentDao.fileToDb();
	}

	@Override
	public boolean levelDbTofile(String path)
	{
		return this.levelDao.dbToFile(path);
	}

	@Override
	public boolean levelFileToDb()
	{
		return this.levelDao.fileToDb();
	}

}
